
package org.soekrat.engine.socraton;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.japril.swing.ExceptionDialog;
import org.japril.swing.TestFrame;

@SuppressWarnings("serial")
public final class BisonServiceDesktop extends JFrame 
{
  private JCheckBox m_ref=new JCheckBox("Reference");
  
  private JTextField m_format=new JTextField(10);
  
  private JTextArea m_data=new JTextArea();
  
  private JLabel m_burl=new JLabel("                   ");
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  private BisonServiceDesktop()
  {
    setTitle("Bison Service");
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    JPanel north=new JPanel(new FlowLayout(FlowLayout.LEFT));
    north.add(m_ref);
    north.add(new JLabel("Format: "));
    north.add(m_format);
    c.add(north, BorderLayout.NORTH);
    
    c.add(new JScrollPane(m_data), BorderLayout.CENTER);
    
    JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel.add(new JButton(m_requestAction));
    panel.add(new JLabel("Bison URL: "));
    m_burl.setEnabled(false);
    panel.add(m_burl);
    c.add(panel, BorderLayout.SOUTH);
    
    pack();
    setSize(500, 500);
  }
  
  private final Action m_requestAction=new AbstractAction("Request")
  {
    @Override
    public void actionPerformed(ActionEvent e)
    {
      try
      {
        boolean ref=m_ref.isSelected();
        String format=m_format.getText();
        String data=m_data.getText();
      
        BisonService bs=new BisonService();
        String burl=bs.data(ref, format, data);
        
        System.out.println("back from service: '"+burl+"'");
        
        m_burl.setText(burl);
      }
      catch (IOException iox)
      {
        ExceptionDialog.showErrorDialog(iox);
      }
    }
  };
  
  
  
  /**
   * <p>
   * </p>
   * @param args
   */
  public static void main(String[] args)
  {
    TestFrame.show(new BisonServiceDesktop());  
  }
  
}
